data(father.son,package = "UsingR")
library(ggplot2)
print(head(father.son))
gg <- ggplot(father.son,aes(x=fheight,y=sheight)) + geom_point() + geom_smooth(method = "lm") + labs(x="Fathers",y="Sons")
print(gg)
heightLM <- lm(sheight ~ fheight,data = father.son)
summary(heightLM)
data(tips,package = "reshape2")
head(tips)
tipsAnova <- aov(tip ~ day - 1,data = tips)
tipsLM <- lm(tip ~ day - 1,data = tips)
summary(tipsAnova)
summary(tipsLM)
library(dplyr)
tipsByDay <- tips %>%
  group_by(day) %>%
  dplyr::summarise(
    tip.mean=mean(tip),tip.sd=sd(tip),
    Length=NROW(tip),
    tfrac=qt(p=.90,df=Length-1),
    Lower=tip.mean - tfrac*tip.sd/sqrt(Length),
    Upper=tip.mean + tfrac*tip.sd/sqrt(Length)
    )

tipsInfo <- summary(tipsLM)
tipsCoef <- as.data.frame(tipsInfo$coefficients[,1:2])
tipsCoef <- within(tipsCoef,{
  Lower <- Estimate - qt(p=0.90,df=tipsInfo$df[2]) * `Std. Error`  
  Upper <- Estimate + qt(p=0.90,df=tipsInfo$df[2]) * `Std. Error`
  day <- rownames(tipsCoef)
})

gg = ggplot(tipsByDay,aes(x=tip.mean,y=day)) + geom_point() + 
geom_errorbarh(aes(xmin=Lower,xmax=Upper),height=.3) + 
ggtitle("Tips by day calculated manually")

print(gg)

gg <- ggplot(tipsCoef,aes(x=Estimate,y=day)) + geom_point() +
  geom_errorbarh(aes(xmin=Lower,xmax=Upper),height=.3) + 
  ggtitle("Tips by day calculated from regression model")

print(gg)


  

  